package com.xsm.yygh.hosp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xsm.yygh.hosp.service.HospitalSetService;
import com.xsm.yygh.model.hosp.HospitalSet;
import com.xsm.yygh.vo.hosp.HospitalSetQueryVo;
import com.xsm.yygy.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@RestController
@RequestMapping("/admin/hosp/hospitalSet")
@Api(tags = "医院设置管理")
@Slf4j
@CrossOrigin    // 允许跨域
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;




    /**
     * 条件查询带分页
     */
    @PostMapping("findPageHospSet/{current}/{limit}")
    @ApiOperation(value = "条件查询带分页")
    public Result findPageHospSet(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {
        //创建page对象，传递当前页，每页记录数
        Page<HospitalSet> page = new Page<>(current,limit);
        //构建条件
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        String hosname = hospitalSetQueryVo.getHosname();//医院名称
        String hoscode = hospitalSetQueryVo.getHoscode();//医院编号
        // 必须导入org.springframework.util.StringUtils吗？
        if(!StringUtils.isEmpty(hosname)) {
            wrapper.like("hosname",hospitalSetQueryVo.getHosname());
        }
        if(!StringUtils.isEmpty(hoscode)) {
            wrapper.eq("hoscode",hospitalSetQueryVo.getHoscode());
        }

        //调用方法实现分页查询
        // ?
        IPage<HospitalSet> pageHospitalSet = hospitalSetService.page(page, wrapper);


        //返回结果
        return Result.ok(pageHospitalSet);
    }


    /**
     * 修改锁定状态
     */
    @PutMapping("/lockHospitalSet/{id}/{status}")
    @ApiOperation(value = "修改锁定状态")
    public Result lockHospitalSet(@PathVariable Long id, @PathVariable Integer status){

        HospitalSet byId = hospitalSetService.getById(id);

        byId.setStatus(status);

        hospitalSetService.updateById(byId);

        return Result.ok();
    }

    /**
     * 根据id获取医院设置
     */
    @GetMapping("getHospSet/{id}")
    @ApiOperation(value = "根据id获取医院设置")
    public Result getHospSet(@PathVariable Long id){
        HospitalSet byId = hospitalSetService.getById(id);
        return Result.ok(byId);
    }


    /**
     * 修改
     */
    @PostMapping("updateById")
    @ApiOperation(value = "updateById")
    public Result updateById(@RequestBody HospitalSet hospitalSet){
        boolean b = hospitalSetService.updateById(hospitalSet);

        if(b){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    /**
     * 添加
     */
    @PostMapping("saveById")
    @ApiOperation(value = "添加")
    public Result saveById(@RequestBody HospitalSet hospitalSet){
        boolean save = hospitalSetService.save(hospitalSet);

        if(save){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }


    /**
     * 批量删除医院设置
     */
    @DeleteMapping("batchRemove")
    @ApiOperation(value = "批量删除医院设置")
    public Result batchRemoveHospitalSet(@RequestBody List<Long> idList) {
        System.err.println(idList);
        for(Long i: idList){
            System.err.println(i);
        }
        hospitalSetService.removeByIds(idList);
        return Result.ok();
    }



    /**
     * 查询医院设置表所有信息
     */
    @GetMapping("findAll")
    @ApiOperation(value = "获取所有医院设置")
    public List<HospitalSet> findAllHospitalSet() {

        // 日志测试
        log.info("hi i'm helen");
        log.warn("warning!!!");
        log.error("it's a error");


        //调用service的方法
        return hospitalSetService.list();
    }



    /**
     * 逻辑删除医院设置
     */
    @ApiOperation(value = "逻辑删除医院设置")
    @DeleteMapping("/{id}")
    public Result removeHospSet(@PathVariable Long id){

        boolean b = hospitalSetService.removeById(id);

        if(b){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
}
